const mongoose = require("mongoose");
const AutoIncrement = require("mongoose-sequence")(mongoose);
const dayjs = require("dayjs");

const Schema = new mongoose.Schema({
    title: { type: String, required: true },
    imageUrl: { type: String, required: true },
    targetUrl: { type: String, required: true },
    order: { type: Number, default: 0, select: false },
    createdAt: { type: String, select: false },
    isActive: { type: Boolean, default: true, select: false }
});

Schema.pre("save", function (next) {
    if (this.isNew) {
        this.createdAt = dayjs().format("YYYY/MM/DD HH:mm:ss");
    }
    next();
});

Schema.plugin(AutoIncrement, { inc_field: "order" });

module.exports = mongoose.model("Banner", Schema);
